package com.mall.seckill.service.impl;

import com.mall.seckill.model.SeckillActivity;
import com.mall.seckill.mapper.SeckillActivityMapper;
import com.mall.seckill.service.ISeckillActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-09-28
 */
@Service
public class SeckillActivityServiceImpl extends ServiceImpl<SeckillActivityMapper, SeckillActivity> implements ISeckillActivityService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addSeckillActivity(SeckillActivity seckillactivity){
        return super.save(seckillactivity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(String id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateSeckillActivity(SeckillActivity seckillactivity){
        return super.updateById(seckillactivity);
    }

    @Override
    public SeckillActivity getById(String id){
        return super.getById(id);
    }

    @Override
    public List<SeckillActivity> selectList(SeckillActivity seckillactivity){
        return super.list(getQueryWrapper(seckillactivity));
    }

    @Override
    public Page<SeckillActivity> selectPage(Page<SeckillActivity> page, SeckillActivity seckillactivity){
        return super.page(page, getQueryWrapper(seckillactivity));
    }

    @Override
    public List<SeckillActivity> validActivity() {
        return baseMapper.validActivity();
    }

    public LambdaQueryWrapper<SeckillActivity> getQueryWrapper(SeckillActivity seckillactivity){
        return Wrappers.<SeckillActivity>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(seckillactivity.getId()), SeckillActivity::getId, seckillactivity.getId())
            .eq(ObjectUtils.isNotEmpty(seckillactivity.getActivityName()), SeckillActivity::getActivityName, seckillactivity.getActivityName())
            .eq(ObjectUtils.isNotEmpty(seckillactivity.getType()), SeckillActivity::getType, seckillactivity.getType())
            .eq(ObjectUtils.isNotEmpty(seckillactivity.getStartTime()), SeckillActivity::getStartTime, seckillactivity.getStartTime())
            .eq(ObjectUtils.isNotEmpty(seckillactivity.getEndTime()), SeckillActivity::getEndTime, seckillactivity.getEndTime())
        ;
    }
}
